package com.css.fxfzfxqh.modules.buildings.calculationTasks.repository.impl;

import com.css.fxfzfxqh.modules.buildingIndividual.entity.BuildingIndividualEntity;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.CalculationTasks;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.FxqhBuildingPriceTaskEntity;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.FxqhHosPeoTaskEntity;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.YSXMainTable;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.repository.CalculationTasksRepository;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.PCC;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.vo.Grid13VO;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.economicLossDE.entity.EconomicLossED;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.entity.ResistEarthquakesDE;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.vo.ResistEarthquakesCalculationVO;
import com.css.fxfzfxqh.modules.seismicHazardData.entity.SettingInfluenceFliedEntity;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/09/20
 * @Description: 建筑物计算任务Repository
 */
@Repository
public class CalculationTasksRepositoryImpl implements CalculationTasksRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    //计算任务表
    private static final String TABLE_NAME = "fxqh_calculation_tasks";

    @Override
    public void addTask(CalculationTasks param) {
        String sql = "insert into " + TABLE_NAME + "(id,assess_code,assess_name,assess_desc,radio2,house_data_flag," +
                "assess_type,assess_obj,ld_pga,seismic_input_mode,house_data,four_probabilistic_versions,influence_field_version," +
                "occurrence_probability_version,beyond_probabilistic_version,ysx_version,year,create_user_level,current_task," +
                "del_flag,create_user,create_time,coordinate_range,popluation_version,gdp_version,assess_province," +
                "assess_city,assess_county,role," +
                "structural_ld_progress,structural_field_progress,structural_four_progress,structural_occurrence_progress,structural_go_beyond_progress," +
                "casualties_ld_progress,casualties_four_progress,casualties_field_progress,casualties_occurrence_progress,casualties_go_beyond_progress," +
                "loss_ld_progress,loss_four_progress,loss_field_progress,loss_occurrence_progress,loss_go_beyond_progress," +
                "dzbqh_structural_ld_progress,dzbqh_structural_four_progress,dzbqh_structural_field_progress,dzbqh_structural_occurrence_progress,dzbqh_structural_go_beyond_progress," +
                "dzbqh_casualties_ld_progress,dzbqh_casualties_four_progress,dzbqh_casualties_field_progress,dzbqh_casualties_occurrence_progress,dzbqh_casualties_go_beyond_progress," +
                "dzbqh_loss_ld_progress,dzbqh_loss_four_progress,dzbqh_loss_field_progress,dzbqh_loss_occurrence_progress,dzbqh_loss_go_beyond_progress," +
                "zhzbqh_ld_progress,zhzbqh_four_progress,zhzbqh_field_progress,zhzbqh_occurrence_progress,zhzbqh_go_beyond_progress,create_user_name) " +
                "values(:id,:assessCode,:assessName,:assessDesc,:radio2,:houseDataFlag," +
                ":assessType,:assessObj,:ldPga,:seismicInputMode,:houseData,:fourProbabilisticVersions,:influenceFieldVersion," +
                ":occurrenceProbabilityVersion,:beyondProbabilisticVersion,:ysxVersion,:year,:createUserLevel,:currentTask," +
                ":delFlag,:createUser,:createTime,:coordinateRange,:popluationVersion,:gdpVersion,:assessProvince," +
                ":assessCity,:assessCounty,:role," +
                ":structuralLdProgress,:structuralFieldProgress,:structuralFourProgress,:structuralOccurrenceProgress,:structuralGoBeyondProgress," +
                ":casualtiesLdProgress,:casualtiesFourProgress,:casualtiesFieldProgress,:casualtiesOccurrenceProgress,:casualtiesGoBeyondProgress," +
                ":lossLdProgress,:lossFourProgress,:lossFieldProgress,:lossOccurrenceProgress,:lossGoBeyondProgress," +
                ":dzbqhStructuralLdProgress,:dzbqhStructuralFourProgress,:dzbqhStructuralFieldProgress,:dzbqhStructuralOccurrenceProgress,:dzbqhStructuralGoBeyondProgress," +
                ":dzbqhCasualtiesLdProgress,:dzbqhCasualtiesFourProgress,:dzbqhCasualtiesFieldProgress,:dzbqhCasualtiesOccurrenceProgress,:dzbqhCasualtiesGoBeyondProgress," +
                ":dzbqhLossLdProgress,:dzbqhLossFourProgress,:dzbqhLossFieldProgress,:dzbqhLossOccurrenceProgress,:dzbqhLossGoBeyondProgress," +
                ":zhzbqhLdProgress,:zhzbqhFourProgress,:zhzbqhFieldProgress,:zhzbqhOccurrenceProgress,:zhzbqhGoBeyondProgress,:createUserName)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void saveHouseDataMonomer(List<BuildingIndividualEntity> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO fxqh_building_individual_copy (uuid,building_code,province,province_id,city,city_id,county,county_id," +
                "longitude,latitude,structure_type1,structure_type2,defence_level,year,floors,usage1,usage2,area,address," +
                "floor_num,ext_wall_thickness,int_wall_thickness,floor_width,floor_length,total_floors,wall_section_area," +
                "land_type,height,floor_height,del_flag,create_name,create_user,create_time,data_version,townorcontry,year50_pro_exceedance002," +
                "year50_pro_exceedance010,year50_pro_exceedance063,year100_pro_exceedance001,task_id,seismic_input_mode,dz_intensity,dz_id)" +
                "values (:uuid,:buildingCode,:province,:provinceId,:city,:cityId,:county,:countyId,:longitude,:latitude,:structureType1," +
                ":structureType2,:defenceLevel,:year,:floors,:usage1,:usage2,:area,:address,:floorNum,:extWallThickness," +
                ":intWallThickness,:floorWidth,:floorLength,:totalFloors,:wallSectionArea,:landType,:height,:floorHeight,:delFlag," +
                ":createName,:createUser,:createTime,:dataVersion,:townorcontry,:year50ProExceedance002," +
                ":year50ProExceedance010,:year50ProExceedance063,:year100ProExceedance001,:taskId,:seismicInputMode,:dzIntensity,:dzId)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }


    @Override
    public void copyData(List<Grid13VO> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO fxqh_grid_data_copy " +
                "(uuid,task_id,province,city,county," +
                "year50_pro_exceedance002,year50_pro_exceedance010,year50_pro_exceedance063,year100_pro_exceedance001," +
                "total_area,f1,f2_f3,f4_f6,f7_f10,f11_f16,f17_f30,f31," +
                "residence,office,business,medic,other," +
                "sf_intensity,sf_intensity6,sf_intensity7,sf_intensity8,sf_intensity9," +
                "structure_type,extends1,area,intensity,geom,dz_intensity,seismic_input_mode,dictionary,dz_id)" +
                "values (:uuid,:taskId,:province,:city,:county," +
                ":year50ProExceedance002,:year50ProExceedance010,:year50ProExceedance063,:year100ProExceedance001," +
                ":totalArea,:f1,:f2F3,:f4F6,:f7F10,:f11F16,:f17F30,:f31," +
                ":residence,:office,:business,:medic,:other," +
                ":sfIntensity,:sfIntensity6,:sfIntensity7,:sfIntensity8,:sfIntensity9," +
                ":structureType,:extends1,:area,:intensity,ST_GeometryFromText(:geom,4490),:dzIntensity,:seismicInputMode,:dictionary,:dzId)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public CalculationTasks findById(String id) {
        String sql = "select * from " + TABLE_NAME + " where id = ?  ";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CalculationTasks.class), id);
    }

    //根据任务id,地震输入方式查询所需数据,dictionary为结构类型
    @Override
    public List<Grid13VO> getLdDate(String id, String seismicInputMode, String dictionary) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from fxqh_grid_data_copy where task_id = '" + id + "'  and extends1 is not null ");
        if (StringUtils.isNotBlank(dictionary)) {
            sql.append(" and dictionary ='" + dictionary + "'");
        }
        if (PlatformObjectUtils.isNotEmpty(seismicInputMode)) {
            sql.append(" and seismic_input_mode = '" + seismicInputMode + "' ");
        }
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Grid13VO.class));
    }

    @Override
    public List<Grid13VO> getLdDate(String id, String seismicInputMode, String dictionary,String province,String city,String area, String influencefliedId) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from fxqh_grid_data_copy where task_id = '" + id + "'  and extends1 is not null ");
        if (StringUtils.isNotBlank(dictionary)) {
            sql.append(" and dictionary ='" + dictionary + "'");
        }
        if (PlatformObjectUtils.isNotEmpty(seismicInputMode)) {
            sql.append(" and seismic_input_mode = '" + seismicInputMode + "' ");
        }
        if(PlatformObjectUtils.isNotEmpty(influencefliedId)){
            sql.append(" and dz_id ='" + influencefliedId + "'");
        }
        sql.append(" and province = '" + province + "' and city='"+city+"' and county='"+area+"' ");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Grid13VO.class));
    }

    @Override
    public List<Grid13VO> getGlGwDate(String id, String dictionary) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from fxqh_grid_data_copy where task_id = '" + id + "'");
        if (StringUtils.isNotBlank(dictionary)) {
            sql.append(" and dictionary ='" + dictionary + "'");
        }
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Grid13VO.class));
    }

    @Override
    public CalculationTasks getTasks(String userID) {
        String sql = "select * from " + TABLE_NAME + " where current_task = '1' and create_user = '" + userID + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CalculationTasks.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    //取消任务
    @Override
    public void cancelTasks(String userID) {
        String sql = " update " + TABLE_NAME + " set current_task = '0' where create_user = '" + userID + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void settingTasks(String id) {
        String sql = " update " + TABLE_NAME + " set current_task = '1' where id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<Grid13VO> getYXCDate(String id, String seismicInputMode, String dictionary, String condition) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from fxqh_grid_data_copy where task_id = '" + id + "' and seismic_input_mode = '" + seismicInputMode + "' ");
        if (StringUtils.isNotBlank(dictionary)) {
            sql.append(" and dictionary ='" + dictionary + "'");
        }
        if (StringUtils.isNotBlank(condition)) {
            sql.append(" and dz_id ='" + condition + "'");
        }
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Grid13VO.class));
    }

    @Override
    public List<BuildingIndividualEntity> getLdDtDate(String id, String seismicInputMode) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from fxqh_building_individual_copy where task_id = '" + id + "' and seismic_input_mode = '" + seismicInputMode + "' ");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BuildingIndividualEntity.class));
    }

    @Override
    public List<BuildingIndividualEntity> getMonomerDate(String id) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from fxqh_building_individual_copy where task_id = '" + id + "'");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BuildingIndividualEntity.class));
    }

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, CalculationTasks param, String createUserLevel) {
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        StringBuilder sql = new StringBuilder("select * from " + TABLE_NAME + " where 1=1  ");
        if (!PlatformObjectUtils.isEmpty(param.getAssessName())) {
            sql.append(" and assess_name like '%" + param.getAssessName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getAssessCode())) {
            sql.append(" and assess_code like '%" + param.getAssessCode() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(createUserLevel)) {
            sql.append(" and create_user_level = '" + createUserLevel + "'");
        }
        //todo  暂时加一下自己查自己评估记录的。
        if (PlatformObjectUtils.isNotEmpty(PlatformSessionContext.getUserID())) {
            sql.append(" and create_user = '" + PlatformSessionContext.getUserID() + "'");
        }
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CalculationTasks.class)).size();
        List<CalculationTasks> list = jdbcTemplate.query(sql.append(orderSql).append(limitSql).toString(), new BeanPropertyRowMapper<>(CalculationTasks.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void deleteCalculation(String id) {
        String sql1 = "delete from fxqh_economic_loss_calculation_ed where assess_id='" + id + "'";
        String sql2 = "delete from fxqh_economic_loss_calculation_pd where assess_id='" + id + "'";
        String sql3 = "delete from fxqh_resist_earthquakes_calculation_ed where assess_id='" + id + "'";
        String sql4 = "delete from fxqh_resist_earthquakes_calculation_pd where assess_id='" + id + "'";
        String sql5 = "delete from fxqh_casualties_calculation_ed where assess_id='" + id + "'";
        String sql6 = "delete from fxqh_casualties_calculation_pd where assess_id='" + id + "'";
        jdbcTemplate.update(sql1);
        jdbcTemplate.update(sql2);
        jdbcTemplate.update(sql3);
        jdbcTemplate.update(sql4);
        jdbcTemplate.update(sql5);
        jdbcTemplate.update(sql6);
    }

    @Override
    public void deleteDataCopy(String id) {
        String sql1 = "delete from fxqh_grid_data_copy where task_id='" + id + "'";
        String sql2 = "delete from fxqh_building_individual_copy where task_id='" + id + "'";
        String sql3 = "delete from jc_ld where assess_id='" + id + "'";
        String sql4 = "delete from jc_pga where assess_id='" + id + "'";
        String sql5 = "delete from fxqh_assess_toponym where assess_id='" + id + "'";
        String sql6 = "delete from fxqh_jzwysx_copy where task_id='" + id + "'";
        jdbcTemplate.update(sql1);
        jdbcTemplate.update(sql2);
        jdbcTemplate.update(sql3);
        jdbcTemplate.update(sql4);
        jdbcTemplate.update(sql5);
        jdbcTemplate.update(sql6);
    }

    @Override
    public void deleteById(String id) {
        String sql = "delete from " + TABLE_NAME + " where id='" + id + "'";
        String sql1 = "delete from fxqh_assess_toponym where assess_id ='" + id + "'";
        jdbcTemplate.update(sql);
        jdbcTemplate.update(sql1);
    }

    @Override
    public void saveFxqhBuildingPriceTask(FxqhBuildingPriceTaskEntity entity) {
        String sql = "INSERT INTO fxqh_building_price_task(uuid,id,province,province_id,city,city_id,county,county_id," +
                "brick_concrete,stone_wood,brick_wood,reinforced_concrete_frame,reinforced_concrete_frame_shear_wall," +
                "reinforced_concrete_shear_wall,soil_wood,wood,steel,industrial_brick_concrete,industrial_reinforced_concrete," +
                "industrial_steel,composite,other,del_flag,create_name,create_user,create_time,update_name,update_user," +
                "update_time,task_id)" +
                " VALUES (:uuid,:id,:province,:provinceId,:city,:cityId,:county,:countyId,:brickConcrete,:stoneWood," +
                ":brickWood,:reinforcedConcreteFrame,:reinforcedConcreteFrameShearWall,:reinforcedConcreteShearWall," +
                ":soilWood,:wood,:steel,:industrialBrickConcrete,:industrialReinforcedConcrete,:industrialSteel,:composite," +
                ":other,:delFlag,:createName,:createUser,:createTime,:updateName,:updateUser,:updateTime,:taskId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void saveFxqhHosPeoTask(FxqhHosPeoTaskEntity entity) {
        String sql = "INSERT INTO fxqh_hos_peo_task(uuid,dhp_id,province,city,prefecture,dhp_town_indoor_property," +
                "dhp_village_indoor_property,create_time,update_time,is_delete,province_id,city_id,prefecture_id," +
                "create_user,update_user,province_code,city_code,prefecture_code,village_town,dhp_town_tatio,task_id)" +
                " VALUES (:uuid,:dhpId,:province,:city,:prefecture," +
                ":dhpTownIndoorProperty,:dhpVillageIndoorProperty,:createTime,:updateTime,:isDelete," +
                ":provinceId,:cityId,:prefectureId,:createUser,:updateUser,:provinceCode,:cityCode," +
                ":prefectureCode,:villageTown,:dhpTownTatio,:taskId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void delFxqhBuildingPriceTask(String taskId) {
        String sql = "delete from fxqh_building_price_task where task_id='" + taskId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void delFxqhHosPeoTask(String taskId) {
        String sql = "delete from fxqh_hos_peo_task where task_id='" + taskId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<FxqhBuildingPriceTaskEntity> getList1ByTaskId(String taskId) {
        StringBuilder sql = new StringBuilder("select * from fxqh_building_price_task where task_id = '" + taskId + "'");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FxqhBuildingPriceTaskEntity.class));
    }

    @Override
    public List<FxqhHosPeoTaskEntity> getList2ByTaskId(String taskId) {
        StringBuilder sql = new StringBuilder("select * from fxqh_hos_peo_task where task_id = '" + taskId + "'");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FxqhHosPeoTaskEntity.class));
    }

    @Override
    public void updateById(CalculationTasks entity) {
        StringBuilder sqlBuilder = new StringBuilder("update fxqh_calculation_tasks set ");
        if (ObjectUtils.isNotEmpty(entity.getId())) {
            sqlBuilder.append("id=:id,");
        }
        if (ObjectUtils.isNotEmpty(entity.getAssessCode())) {
            sqlBuilder.append("assess_code=:assessCode,");
        }
        if (ObjectUtils.isNotEmpty(entity.getAssessName())) {
            sqlBuilder.append("assess_name=:assessName,");
        }
        if (ObjectUtils.isNotEmpty(entity.getAssessDesc())) {
            sqlBuilder.append("assess_desc=:assessDesc,");
        }
        if (ObjectUtils.isNotEmpty(entity.getRadio2())) {
            sqlBuilder.append("radio2=:radio2,");
        }
        if (ObjectUtils.isNotEmpty(entity.getHouseDataFlag())) {
            sqlBuilder.append("house_data_flag=:houseDataFlag,");
        }
        if (ObjectUtils.isNotEmpty(entity.getAssessType())) {
            sqlBuilder.append("assess_type=:assessType,");
        }
        if (ObjectUtils.isNotEmpty(entity.getAssessObj())) {
            sqlBuilder.append("assess_obj=:assessObj,");
        }
        if (ObjectUtils.isNotEmpty(entity.getLdPga())) {
            sqlBuilder.append("ld_pga=:ldPga,");
        }
        if (ObjectUtils.isNotEmpty(entity.getSeismicInputMode())) {
            sqlBuilder.append("seismic_input_mode=:seismicInputMode,");
        }
        if (ObjectUtils.isNotEmpty(entity.getHouseData())) {
            sqlBuilder.append("house_data=:houseData,");
        }
        if (ObjectUtils.isNotEmpty(entity.getFourProbabilisticVersions())) {
            sqlBuilder.append("four_probabilistic_versions=:fourProbabilisticVersions,");
        }
        if (ObjectUtils.isNotEmpty(entity.getInfluenceFieldVersion())) {
            sqlBuilder.append("influence_field_version=:influenceFieldVersion,");
        }
        if (ObjectUtils.isNotEmpty(entity.getOccurrenceProbabilityVersion())) {
            sqlBuilder.append("occurrence_probability_version=:occurrenceProbabilityVersion,");
        }
        if (ObjectUtils.isNotEmpty(entity.getStructuralGoBeyondProgress())) {
            sqlBuilder.append("beyond_probabilistic_version=:beyondProbabilisticVersion,");
        }
        if (ObjectUtils.isNotEmpty(entity.getYsxVersion())) {
            sqlBuilder.append("ysx_version=:ysxVersion,");
        }
        if (ObjectUtils.isNotEmpty(entity.getYear())) {
            sqlBuilder.append("year=:year,");
        }
        if (ObjectUtils.isNotEmpty(entity.getCreateUserLevel())) {
            sqlBuilder.append("create_user_level=:createUserLevel,");
        }
        if (ObjectUtils.isNotEmpty(entity.getCurrentTask())) {
            sqlBuilder.append("current_task=:currentTask,");
        }
        if (ObjectUtils.isNotEmpty(entity.getDelFlag())) {
            sqlBuilder.append("del_flag=:delFlag,");
        }
        if (ObjectUtils.isNotEmpty(entity.getCreateUser())) {
            sqlBuilder.append("create_user=:createUser,");
        }
        if (ObjectUtils.isNotEmpty(entity.getCreateTime())) {
            sqlBuilder.append("create_time=:createTime,");
        }
        if (ObjectUtils.isNotEmpty(entity.getUpdateUser())) {
            sqlBuilder.append("update_user=:updateUser,");
        }
        if (ObjectUtils.isNotEmpty(entity.getUpdateTime())) {
            sqlBuilder.append("update_time=:updateTime,");
        }
        if (ObjectUtils.isNotEmpty(entity.getAreaRange())) {
            sqlBuilder.append("area_range=:areaRange,");
        }
        if (ObjectUtils.isNotEmpty(entity.getPopluationVersion())) {
            sqlBuilder.append("popluation_version=:popluationVersion,");
        }
        if (ObjectUtils.isNotEmpty(entity.getGdpVersion())) {
            sqlBuilder.append("gdp_version=:gdpVersion,");
        }
        if (ObjectUtils.isNotEmpty(entity.getCoordinateRange())) {
            sqlBuilder.append("coordinate_range=:coordinateRange,");
        }
        sqlBuilder.deleteCharAt(sqlBuilder.lastIndexOf(","));
        sqlBuilder.append(" where id=:id");
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sqlBuilder.toString(), source);
    }

    //根据省市县和版本查询易损性
    @Override
    public List<YSXMainTable> getfxqhJzwysx(PCC pcc, String ysxVersion) {
        StringBuilder sql = new StringBuilder("select * from fxqh_jzwysx where ysx_type = '" + ysxVersion + "'");
        if(PlatformObjectUtils.isNotEmpty(pcc)){
            if(StringUtils.isNotBlank(pcc.getPpName())){
                sql.append(" and province = '" + pcc.getPpName() + "' ");
            }
            if(StringUtils.isNotBlank(pcc.getPpName())){
                sql.append(" and city = '" + pcc.getCityName() + "' ");
            }
            if(StringUtils.isNotBlank(pcc.getPpName())){
                sql.append(" and county = '" + pcc.getName() + "' ");
            }
        }
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(YSXMainTable.class));
    }

    //复制易损性数据
    @Override
    public void copyYSXData(YSXMainTable ysxMainTable) {
        //复制主表信息
        String sql = "insert into fxqh_jzwysx_copy (id,province,city,county,structure_type1,structure_type2," +
                "fortify,year,housing_use1,housing_use2,number_of_floors,ysx_type,create_user,create_time,task_id) " +
                "values(:id,:province,:city,:county,:structureType1,:structureType2," +
                ":fortify,:year,:housingUse1,:housingUse2,:numberOfFloors,:ysxType,:createUser,:createTime,:taskId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(ysxMainTable);
        namedParameterJdbcTemplate.update(sql, source);
    }

    //根据任务id查询所有易损性
    @Override
    public List<YSXMainTable> getYSXByTaskId(String id) {
        StringBuilder sql = new StringBuilder("select * from fxqh_jzwysx_copy where task_id = '" + id + "'");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(YSXMainTable.class));
    }

    //删除复制的易损性数据
    @Override
    public void deleteYSXData(YSXMainTable ysxMainTable) {
        String sql1 = "delete from fxqh_jzwysx_copy where id ='" + ysxMainTable.getId() + "'";
        String sql2 = "delete from fxqh_jzwysx_sub_copy where pid ='" + ysxMainTable.getId() + "'";
        jdbcTemplate.update(sql1);
        jdbcTemplate.update(sql2);
    }

    @Override
    public List<YSXMainTable> getysxValue(String id) {
        StringBuilder sql = new StringBuilder("select * from fxqh_jzwysx_sub where pid = '" + id + "'");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(YSXMainTable.class));
    }

    @Override
    public void copyYSXValue(YSXMainTable mainTable) {
        //复制子表信息
        String sql = "insert into fxqh_jzwysx_sub_copy (pid,intensity,damage,rate) " +
                "values(:pid,:intensity,:damage,:rate)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(mainTable);
        namedParameterJdbcTemplate.update(sql, source);
    }

    //更新发生概率危险性计算进度为计算完成
    @Override
    public void updateFSProgress(String id, String type, String obj) {
        StringBuilder sql = new StringBuilder();
        if (type.equals("评估")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set structural_occurrence_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set casualties_occurrence_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set loss_occurrence_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("单指标区划")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_structural_occurrence_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_casualties_occurrence_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_loss_occurrence_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("综合指标区划")) {
            sql.append(" update " + TABLE_NAME + " set zhzbqh_occurrence_progress = '1' where id = '" + id + "'");
        }
        jdbcTemplate.update(sql.toString());
    }

    ////更新超越概率危险性计算进度为计算完成
    @Override
    public void updateCYProgress(String id, String type, String obj) {
        StringBuilder sql = new StringBuilder();
        if (type.equals("评估")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set structural_go_beyond_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set casualties_go_beyond_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set loss_go_beyond_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("单指标区划")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_structural_go_beyond_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_casualties_go_beyond_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_loss_go_beyond_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("综合指标区划")) {
            sql.append(" update " + TABLE_NAME + " set zhzbqh_go_beyond_progress = '1' where id = '" + id + "'");
        }
        jdbcTemplate.update(sql.toString());
    }

    //更新烈度计算任务为抗震能力计算完成
    @Override
    public void updateLdProgress(String id, String type, String obj) {
        StringBuilder sql = new StringBuilder();
        if (type.equals("评估")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set structural_ld_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set casualties_ld_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set loss_ld_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("单指标区划")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_structural_ld_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_casualties_ld_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_loss_ld_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("综合指标区划")) {
            sql.append(" update " + TABLE_NAME + " set zhzbqh_ld_progress = '1' where id = '" + id + "'");
        }
        jdbcTemplate.update(sql.toString());
    }

    @Override
    public void updateFourProbabilisticProgress(String id, String type, String obj) {
        StringBuilder sql = new StringBuilder();
        if (type.equals("评估")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set structural_four_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set casualties_four_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set loss_four_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("单指标区划")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_structural_four_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_casualties_four_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_loss_four_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("综合指标区划")) {
            sql.append(" update " + TABLE_NAME + " set zhzbqh_four_progress = '1' where id = '" + id + "'");
        }
        jdbcTemplate.update(sql.toString());
    }

    @Override
    public void updateStructuralFieldProgress(String id, String type, String obj) {
        StringBuilder sql = new StringBuilder();
        if (type.equals("评估")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set structural_field_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set casualties_field_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set loss_field_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("单指标区划")) {
            if (obj.equals("结构破坏")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_structural_field_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("人员伤亡")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_casualties_field_progress = '1' where id = '" + id + "'");
            }
            if (obj.equals("经济损失")) {
                sql.append(" update " + TABLE_NAME + " set dzbqh_loss_field_progress = '1' where id = '" + id + "'");
            }
        }
        if (type.equals("综合指标区划")) {
            sql.append(" update " + TABLE_NAME + " set zhzbqh_field_progress = '1' where id = '" + id + "'");
        }
        jdbcTemplate.update(sql.toString());
    }

    @Override
    public boolean verification(String areaRange) {
        try {
            String sql = "select st_isvalid('MULTIPOLYGON(((" + areaRange + ")))')";
            String s = jdbcTemplate.queryForObject(sql, String.class);
            if (s.equals("t")) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<String> getAllTask(String provinceId) {
        String sql = "";
        if("大屏展示".equals(provinceId)){
            sql = "select id from fxqh_calculation_tasks t where t.assess_name like '%大屏展示%'";
        }else{
            sql = "SELECT t.id FROM fxqh_assess_toponym a inner join fxqh_calculation_tasks t on t.id = a.assess_id and  t.assess_name like '%测试%' and a.pid='"+provinceId+"'";
        }
        return jdbcTemplate.queryForList(sql, String.class);
    }
}
